import { DataTypes, Model } from 'sequelize'
import { sequelize } from '../../connect'
import { findAllValues, findOneRandomValue, findOneValue } from '../../public'

type StoryOption = {
  text: string // 选项内容
  next_id: string // 跳转到的剧情id
  condition?: string // 可选：触发条件
}

type ModelProps = {
  id: number
  story_id: string // 剧情唯一标识
  title: string // 剧情标题
  content: string // 剧情内容
  things?: string[] // 可选：剧情中的事件
  // 跳转剧情{ "type": "跳转剧情", "value": "剧情ID" }
  // 种族初始化{ "type": "种族初始化", "value": "种族" }
  // 种族词条{ "type": "种族词条", "value": [{"race_name": "", "affixes": ""}]}
  // 接受任务{ "type": "接受任务", "value": [{"id": "", "name": , "aim": , "reward": }]}
  // 进入战斗{ "type": "进入战斗", "value": [{"name": "", "race_name": "", "level": "", "affixes": "", "equipped": "", "position": ""}] }
  // 获得物品{ "type": "获得物品", "value": [{"name": "", "number": ""}]}

  options: StoryOption[] // 选项数组
  status: number // 状态（如启用/禁用）
}

class main_story extends Model<ModelProps> {
  public static findAllValues = findAllValues
  public static findOneValue = findOneValue
  public static findOneRandomValue = findOneRandomValue
}

main_story.init(
  {
    id: {
      type: DataTypes.BIGINT,
      primaryKey: true,
      allowNull: false,
      autoIncrement: true,
      comment: '自增主键'
    },
    story_id: {
      type: DataTypes.STRING(100),
      allowNull: false,
      unique: true,
      comment: '剧情唯一标识'
    },
    title: {
      type: DataTypes.STRING(100),
      allowNull: false,
      comment: '剧情标题'
    },
    content: {
      type: DataTypes.TEXT,
      allowNull: false,
      comment: '剧情内容'
    },
    things: {
      type: DataTypes.JSON,
      allowNull: true,
      comment: '剧情中的物品或事件，格式为[{type, name, value}]'
    },
    options: {
      type: DataTypes.JSON,
      allowNull: false,
      comment: '选项数组，每项包含text和next_id'
    },
    status: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 1,
      comment: '状态 1启用 0禁用'
    }
  },
  {
    sequelize,
    tableName: 'story',
    freezeTableName: true,
    createdAt: false,
    updatedAt: false,
    indexes: [
      {
        unique: true,
        fields: ['id']
      },
      {
        unique: true,
        fields: ['story_id']
      }
    ]
  }
)

export { main_story }
